package org.raymond.iworks.study.netty.atguigu.netty.codec;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
import org.raymond.iworks.study.netty.atguigu.netty.codec.model.Student;

/**
 * @author raymond
 * @version V1.0
 * @Description: TODO
 */
@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<Student> {
    @Override
    public void channelRead0(ChannelHandlerContext ctx, Student student) throws Exception {
        log.info("{}->: {}", ctx.channel().remoteAddress().toString(), student);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello, 客户端".getBytes(CharsetUtil.UTF_8)));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.error("{}", cause.getMessage());
        ctx.close();
    }
}
